package com.itheima.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @author: dh
 * @date: 2024-01-03 15:57
 * 创建Spring的AOP通知类.
 */
@Component
@Aspect
public class ProjectAdvice {

    @Pointcut("execution(* com.itheima.service.*Service.*(..))")
    private void servicePt() {}

    //@Around("ProjectAdvice.servicePt()") 可以简写为下面的方式
    @Around("servicePt()")
    public void runSpeed(ProceedingJoinPoint pjp) throws Throwable {

        // 1.获取执行签名信息.
        Signature signature = pjp.getSignature();
        // 2.获得接口名
        String className = signature.getDeclaringTypeName();
        // 3.获得方法名.
        String methodName = signature.getName();
        long start = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {

            pjp.proceed();
        }
        long end = System.currentTimeMillis();
        System.out.println("万次执行时间: "+ className + "." + methodName + ": " +(end-start)+"ms");
    }
}
